
package test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.solosky.kindlepush.forum.bean.Post;
import net.solosky.kindlepush.forum.bean.Topic;
import net.solosky.kindlepush.forum.book.BookBuilder;
import net.solosky.kindlepush.forum.book.EpubBookBuilder;
import net.solosky.kindlepush.forum.provider.DoubanForumProvider;
import net.solosky.kindlepush.forum.provider.ForumProvider;

public class BookBuilderTest2 {
	public static void main(String[] args) throws Exception {
		
//		EpubReader epubReader = new EpubReader();
//		Book book = epubReader.readEpub(new FileInputStream("testpdf.epub"));
//		System.out.println(book.getTitle());
		//System.exit(1);
		
		ForumProvider provider = new DoubanForumProvider();
		Topic topic = provider.parseTopic("http://www.douban.com/group/topic/27866438/");
		BookBuilder builder = new EpubBookBuilder();
		builder.writeTopic("书籍封面", topic);
		
		int floor = 0;
		int page  = 0;
		
		for(String url: topic.getPageUrls()){
			System.out.println("FETCH POSTS:"+url);
			List<Post> posts;
			try {
				posts = provider.parsePosts(url);
			} catch (Exception e) {
				posts = provider.parsePosts(url);
			}
			
			List<Post> lzPosts = new ArrayList<Post>();
			List<Post> allPosts = new ArrayList<Post>();
			for(Post post:posts){
				post.setFloor(++floor);
				if(post.getAuthor().equals(topic.getAuthor())){
					lzPosts.add(post);
				}
				if(post.getText().length()>20){
					allPosts.add(post);
				}
			}
			page++;
			
			builder.writePosts("帖子正文", "帖子第"+(page)+"页", allPosts);
			builder.writePosts("只看楼主", "帖子第"+(page)+"页", lzPosts);
			
			
		}
		try{
			builder.saveBook(new File(topic.getTitle()+".epub"));
		}catch(Throwable t){
			t.printStackTrace();
		}
	}
}
